package com.android.dumiaogou.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.dumiaogou.R;
import com.android.dumiaogou.adapt.listMineMoneyAdapt;
import com.android.dumiaogou.adapt.listMineMoneyRecordAdapt;
import com.android.dumiaogou.fragment.BaseLazyFragment;
import com.android.dumiaogou.model.member.CashData;
import com.android.dumiaogou.model.member.CashDataCallback;
import com.android.dumiaogou.model.member.CashRecordData;
import com.android.dumiaogou.model.member.CashRecordDataCallback;
import com.android.dumiaogou.utils.ConstantsUrl;
import com.android.dumiaogou.utils.HttpUtils;
import com.android.dumiaogou.utils.LoginState;
import com.android.dumiaogou.utils.MyLog;
import com.android.dumiaogou.utils.ObjectUtils;
import com.android.dumiaogou.view.recycleview.CustomLoadMoreView;
import com.android.dumiaogou.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 佣金与提现记录
 */
public class Fragment_Mine_Money extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;

    private static final String ARG_PARAM1 = "param1";
    private int type;
    private boolean networkConnected;

    private int page = 1;
    private TextView price;


    private listMineMoneyRecordAdapt adapt_log;
    private String commission;
    private listMineMoneyAdapt adapter;

    public Fragment_Mine_Money() {


    }


    public static Fragment_Mine_Money newInstance(int i) {
        Fragment_Mine_Money fragment = new Fragment_Mine_Money();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            switch (type) {
                case 0:
                    GetDataListData(page);
                    break;
                case 1:
                    GetDataListDataCashLog(page);
                    break;
            }
        } else {

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, view);
        mcontext = getActivity();
        mainTopBg.setVisibility(View.GONE);

        return view;
    }


    private void initUI() {
        View fragment_head = inflater.inflate(R.layout.head_mine_money, null, false);
        price = (TextView) fragment_head.findViewById(R.id.price);
        TextView price_type = (TextView) fragment_head.findViewById(R.id.price_type);
        View cash_record_top = fragment_head.findViewById(R.id.cash_record_top);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        switch (type) {
            case 0:
                price_type.setText("佣金总额（元）");
                adapter = new listMineMoneyAdapt(R.layout.item_list_mine_money, null);
                adapter.addHeaderView(fragment_head);
                adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                adapter.setLoadMoreView(new CustomLoadMoreView());
                adapter.setOnLoadMoreListener(this, mRecyclerView);
                mRecyclerView.setAdapter(adapter);
                break;
            case 1:
                price_type.setText("累计提现（元）");
                cash_record_top.setVisibility(View.VISIBLE);
                adapt_log = new listMineMoneyRecordAdapt(R.layout.item_list_mine_money_record, null);
                adapt_log.addHeaderView(fragment_head);
                adapt_log.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                adapt_log.setLoadMoreView(new CustomLoadMoreView());
                adapt_log.setOnLoadMoreListener(this, mRecyclerView);
                mRecyclerView.setAdapter(adapt_log);
                break;
        }


        ReFreshData();


    }

    private void ReFreshData() {

        //设置颜色
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        //自动刷新


        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                networkConnected = ObjectUtils.isNetworkConnected(getActivity());
                if (networkConnected) {
                    page = 1;
                    switch (type) {
                        case 0:
                            GetDataListData(page);
                            break;
                        case 1:
                            GetDataListDataCashLog(page);
                            break;
                    }


                } else {

                    ObjectUtils.toast(getActivity(), "网络连接出现异常");
                    mRefreshLayout.setRefreshing(false);
                }


            }
        });


    }

    //获取列表数据
    private void GetDataListData(final int page) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERMONEY + "?page=" + page;


        MyLog.e("地址", address);
        if (networkConnected) {
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new CashDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(CashData response, int id) {

                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {
                        String commission_total = response.getData().getCommission_total();
                        commission = response.getData().getCommission();
                        price.setText(commission_total);

                        ArrayList<CashData.DataBean.ListBean> listdata = new ArrayList<>();

                        if (response.getData() != null && response.getData().getList().size() > 0) {
                            listdata.addAll(response.getData().getList());
                            if (page == 1) {
                                adapter.setNewData(listdata);
                            } else {
                                adapter.addData(listdata);
                                adapter.loadMoreComplete();
                            }
                        } else {
                            adapter.loadMoreEnd();
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
            adapter.loadMoreFail();
        }
    }
    private void GetDataListDataCashLog(final int page) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERMONEY_CASH_LOG + "?page=" + page;

        MyLog.e("地址", address);
        if (networkConnected) {
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new CashRecordDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(CashRecordData response, int id) {

                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {
                        String total = (String) response.getData().getTotal();
                        price.setText(total);

                        ArrayList<CashRecordData.DataBean.ListBean> listdata = new ArrayList<>();
                        if (response.getData().getList().size() == 0 && page == 1) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        if (response.getData() != null && response.getData().getList().size() > 0) {
                            listdata.addAll(response.getData().getList());
                            if (page == 1) {
                                adapt_log.setNewData(listdata);
                            } else {
                                adapt_log.addData(listdata);
                                adapt_log.loadMoreComplete();
                            }
                        } else {
                            adapt_log.loadMoreEnd();
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }

                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
            adapt_log.loadMoreFail();

        }
    }


    @OnClick({R.id.click_resetnetwork_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                page = 1;
                switch (type) {
                    case 0:
                        GetDataListData(page);
                        break;
                    case 1:
                        GetDataListDataCashLog(page);
                        break;
                }
                break;

        }
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        switch (type) {
            case 0:
                GetDataListData(page);
                break;
            case 1:
                GetDataListDataCashLog(page);
                break;
        }

    }
}
